package com.ktwlsoft.archivemanage.controller;

import com.ktwlsoft.archivemanage.domain.business.repositoryService.InforConstRepositoryService;
import com.ktwlsoft.archivemanage.payload.Result;
import com.ktwlsoft.archivemanage.payload.ResultType;
import com.ktwlsoft.archivemanage.payload.inforConst.InforConstAddRequest;
import com.ktwlsoft.archivemanage.payload.inforConst.InforConstUpdateRequest;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "/inforConst")
public class InforConstController {

    @Autowired
    private InforConstRepositoryService inforConstRepositoryService;

    @ApiOperation(value = "查询信息化建设")
    @RequestMapping(value = "/findOne",produces = "application/json",method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<?> findInforConstItem(@ApiParam(name = "unitId",value = "立档单位id")
                                                @RequestParam(defaultValue = "",required = true) Long unitId){
        Result result=inforConstRepositoryService.findInforConstItem(unitId);
        return ResponseEntity.ok(result);
    }

    @ApiOperation(value = "新增信息化建设")
    @RequestMapping(value = "/addOne",produces = "application/json",method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<?> addInforConstItem(@RequestBody InforConstAddRequest request,
                                               BindingResult bindingResult){
        Result result=null;
        if(bindingResult.hasErrors()){
            result=Result.body(request, ResultType.CHECK_ERROR);
            result.withMessage(bindingResult.getFieldError().getDefaultMessage());
            return ResponseEntity.ok(result);
        }
        result=inforConstRepositoryService.addInforConstItem(request);
        return ResponseEntity.ok(result);
    }

    @ApiOperation(value = "更新信息化建设")
    @RequestMapping(value = "/updateOne",produces = "application/json",method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<?> updateInforConstItem(@RequestBody InforConstUpdateRequest request,
                                                  BindingResult bindingResult){
        Result result=null;
        if(bindingResult.hasErrors()){
            result=Result.body(request, ResultType.CHECK_ERROR);
            result.withMessage(bindingResult.getFieldError().getDefaultMessage());
            return ResponseEntity.ok(result);
        }
        result=inforConstRepositoryService.updateInforConstItem(request);
        return ResponseEntity.ok(result);
    }

}
